package controller;

//TODO comment for the class

import gui.Display;
import gui.SettingsDisplay;
import gui.StatisticsDisplay;

public class StatisticsController extends Controller {
	//TODO comment for constructors
	public StatisticsController(Display display) {
		StatisticsDisplay statisticsDisplay = new StatisticsDisplay(this, display);
		setDisplay(statisticsDisplay);
	}
	
	public StatisticsController(Display display, boolean initializeDisplay) {
		if (initializeDisplay) {
			StatisticsDisplay statisticsDisplay = new StatisticsDisplay(this, display);
			setDisplay(statisticsDisplay);
		}
	}

	/**
	 * Update the user's statistics to display them all on the statistics page
	 * @param display TODO decribe this
	 */
	public void initializeDisplay(Display display) {
		StatisticsDisplay statisticsDisplay = new StatisticsDisplay(this, display);
		setDisplay(statisticsDisplay);
		
		//update the username
		statisticsDisplay.updateUsername(user.getUsername());
		
		//update the user's balance
		statisticsDisplay.updateBank(user.getUserBank().getBalance());
		
		//get the user's current exp points, the points to the next rank and the point needed to reach that rank
		int currentExp = user.getUserStatistics().getExperiencePoints();
		int nextRankExp = user.getUserStatistics().getRank().getNextLevelPoints();
		int requiredExp = nextRankExp - currentExp;
		
		//update the user's rank and experience point information
		statisticsDisplay.updateRank(user.getUserStatistics().getRank().getRank());
		statisticsDisplay.updateExp(currentExp);
		statisticsDisplay.updateNextExp(requiredExp);
		statisticsDisplay.setProgress(currentExp, nextRankExp);
		
		//get the user's number of hand played, won and lost
		int handsPlayed = user.getUserStatistics().getHandsPlayed();
		int handsWon = user.getUserStatistics().getHandsWon();
		int handsLost = handsPlayed-handsWon;
		
		/* if the user has played a hand calculate the win percentage
		 * otherwise do not calculate it to avoid a divide by 0 error
		 */
		if(handsPlayed != 0){
			double winPercentage = ((double) handsWon/(double) handsPlayed)*100;
			statisticsDisplay.updatePercentage(winPercentage);
		}
		
		//update the user's hand statistics
		statisticsDisplay.updateHandsPlayed(handsPlayed);
		statisticsDisplay.updateHandsWon(handsWon);
		statisticsDisplay.updateHandsLost(handsLost);
		
	}

	/**
	 * Return to the lobby screen by giving control back to the lobby controller
	 */
	public void returnToLobby() {
		Controller lobbyController = new LobbyController(display);
		this.giveControlTo(lobbyController);
	}
}
